package yiran.dcolormotd.manager;

import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import yiran.dcolormotd.DColorMotd;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MotdManager {
    private final Server server;
    private final ConfigManager configManager;
    private final DColorMotd plugin;

    public MotdManager(JavaPlugin plugin, ConfigManager configManager) {
        this.server = plugin.getServer();
        this.configManager = configManager;
        this.plugin = (DColorMotd) plugin; // 添加这一行
    }

    public String getProcessedMotd() {
        FileConfiguration config = configManager.getConfig();
        List<String> motdLines;

        if (config.getBoolean("random_motd", false)) {
            List<?> motdGroupsRaw = config.getList("random_motds");
            motdLines = getRandomMotdLines(motdGroupsRaw);
        } else {
            motdLines = config.getStringList("motd");
        }

        if (motdLines.isEmpty()) {
            motdLines = new ArrayList<>();
            motdLines.add("&a默认MOTD");
        }

        // 获取标题
        String title = config.getString("title", "&a&l我的服务器");
        title = processLine(title); // 单独处理标题行

        // 处理MOTD内容
        StringBuilder contentBuilder = new StringBuilder();
        for (String line : motdLines) {
            contentBuilder.append(processLine(line)).append("\n");
        }

        // 移除最后一个换行符
        if (!contentBuilder.isEmpty()) {
            contentBuilder.setLength(contentBuilder.length() - 1);
        }

        // 组合标题和内容
        return title + "\n" + contentBuilder;
    }

    private String processLine(String line) {
        String processedLine = line;

        // 1.16+ 支持十六进制颜色代码
        if (DColorMotd.isVersionAtLeast(1, 16)) {
            processedLine = processedLine.replace("&#", "<#");
        }

        // 替换变量 - 使用兼容方法获取玩家数量
        processedLine = processedLine
                .replace("%players%", String.valueOf(DColorMotd.getOnlinePlayersCount()))
                .replace("%max_players%", String.valueOf(server.getMaxPlayers()))
                .replace("%version%", server.getBukkitVersion());

        // 1.12.2及以下版本使用旧版颜色代码
        if (!DColorMotd.isVersionAtLeast(1, 13)) {
            processedLine = ChatColor.translateAlternateColorCodes('&', processedLine);
        }

        // 长度限制
        int maxLength = configManager.getConfig().getInt("max_length", 100);
        if (processedLine.length() > maxLength) {
            processedLine = processedLine.substring(0, maxLength);
            server.getLogger().warning("MOTD行长度超过限制，已截断: " + line);
        }

        return processedLine;
    }

    private List<String> getRandomMotdLines(List<?> motdGroupsRaw) {
        if (motdGroupsRaw == null || motdGroupsRaw.isEmpty()) {
            return new ArrayList<>();
        }

        Random random = new Random();
        int index = random.nextInt(motdGroupsRaw.size());

        Object groupObj = motdGroupsRaw.get(index);
        if (groupObj instanceof List<?> innerList) {
            List<String> stringList = new ArrayList<>();
            for (Object item : innerList) {
                if (item instanceof String) {
                    stringList.add((String) item);
                }
            }
            return stringList;
        }
        return new ArrayList<>();
    }

    public DColorMotd getPlugin() {
        return plugin;
    }
}

